<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends CommonController {

    //显示用户列表
    public function UserList(){
        $user=M('user');
        //$this->coachlist=$coach->select();
        $this->UserList=$user->select();
        //var_dump($this);

        //显示编号
        $cnt=1;
        $this->assign('cnt',$cnt);

        $this->display();
    }

    //显示教师列表
    public function TeacherList(){
        $user=M('user');
        //$this->coachlist=$coach->select();
        $this->TeacherList=$user->where("u_state ='1'" )->select();
        //var_dump($this);

        //显示编号
        $cnt=1;
        $this->assign('cnt',$cnt);

        $this->display();
    }

    //显示学生列表
    public function StudentList(){
        $user=M('user');
        //$this->coachlist=$coach->select();
        $this->StudentList=$user->where("u_state ='0'" )->select();
        //var_dump($this);

        //显示编号
        $cnt=1;
        $this->assign('cnt',$cnt);

        $this->display();
    }




//重置密码
    public function UserPwd($user_id)
    {
        //echo $coach_id;
        $data['u_pwd']=md5('123456');
        $user=M('user');
        $res=$user->where("u_id=$user_id")->save($data);
        // echo $coach->getLastSql();//显示执行的sql语句
        // echo $res;
        if(false!== $res){
            $this->success('重置密码成功！');
        }else{
            $this->error('重置密码失败！');
        }

    }

    //删除用户
    public function UserDel($user_id){

        $User = M('user');

        $photoName = ($User->select($user_id))['0']['u_pic'];

        if ($photoName != 'default.png') { //如果不是默认文件
            unlink('./Public/upload/userpics/' . $photoName); //删除对应文件
        }
        if ($User->delete($user_id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    //删除用户详情
    public function UserInfoDel($user_id){

        $User = M('user');

        $photoName = ($User->select($user_id))['0']['u_pic'];

        if ($photoName != 'default.png') { //如果不是默认文件
            unlink('./Public/upload/userpics/' . $photoName); //删除对应文件
        }
        if ($User->delete($user_id)) {
            $this->success('删除成功','/zxdyxt_php/Admin/User/UserList', 3);
        } else {
            $this->error('删除失败');
        }

    }

    //设置用户类型为教师
    public function state(){
        $id=$_GET['id'];
        // echo $id;
        $data['u_state']='1';
        $usersModel=M('user');

        if(false!== $usersModel->where("u_id=$id")->save($data)){
            $this->success('设置教师用户成功！');
        }else{
            $this->error('设置教师用户失败！');
        }
    }

    //设置用户类型为学生
    public function isstate(){
        $id=$_GET['id'];
        // echo $id;
        $data['u_state']='0';
        $usersModel=M('user');

        if(false!== $usersModel->where("u_id=$id")->save($data)){
            $this->success('设置学生用户成功！');
        }else{
            $this->error('设置学生用户失败！');
        }

    }
    //显示用户详情+用户提问列表+用户回答列表
    public function UserInfo($user_id){

        $user=M('user');
        $this -> UserInfo = $user -> select($user_id);

        //显示用户问题列表
        $question=M('question');
        $this -> QuestionList = $question
            ->join('user on user.u_id=question.q_u_id')->where("q_u_id=$user_id")->select();

        //显示用户回答列表
        $answer=M('answer');
        $this -> AnswerList = $answer
            ->join('question on question.q_id=answer.an_q_id')
            ->join('user on user.u_id=answer.an_u_id')->where("an_u_id=$user_id")->select();

        //dump($this -> QuestionInfo);

        $this -> display();

    }

    //删除问题
    public function QuestionDel($q_id){

        $Question = M('question');

        $photoName = ($Question->select($q_id))['0']['q_pic'];

        if ($photoName != 'default.png') { //如果不是默认文件
            unlink('./Public/upload/questionpics/' . $photoName); //删除对应文件
        }
        if ($Question->delete($q_id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    //删除回答
    public function AnswerDel($an_id){

        $Answer = M('user');

        $photoName = ($Answer->select($an_id))['0']['an_pic'];

        if ($photoName != 'default.png') { //如果不是默认文件
            unlink('./Public/upload/answerpics/' . $photoName); //删除对应文件
        }
        if ($Answer->delete($an_id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }






}